package com.supcon.exchangedata.service.gj;

import com.supcon.exchangedata.pojo.entity.AjaxMessage;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.SgjjtBikeTrade;
import com.supcon.exchangedata.pojo.entity.oracle.sync.CtGjBikeTrade;
import com.supcon.exchangedata.repository.master.MasterSgjjtBikeTradeRepository;
import com.supcon.exchangedata.repository.slave4.SlaveGjBasicInfo7;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SgjjtBikeTradeService {
    @Resource
    private SlaveGjBasicInfo7 slaveGjBasicInfo7;
    @Resource
    private MasterSgjjtBikeTradeRepository masterSgjjtBikeTradeRepository;
    @Resource
    private RestTemplate restTemplate;

    public void newGjBikeTradeList(){
        List<SgjjtBikeTrade> all = slaveGjBasicInfo7.findAll();
        List<CtGjBikeTrade> objects = new ArrayList<>();
        if (!CollectionUtils.isEmpty(all)){
            all.forEach(s->{
                CtGjBikeTrade bike = new CtGjBikeTrade();
                BeanUtils.copyProperties(s,bike);
                bike.setId(s.getTongid());
                objects.add(bike);
            });
            this.masterSgjjtBikeTradeRepository.saveAll(objects);
        }

    }
    public void acquireBike(){
        String url ="http://10.101.22.124:8001/dpm/proxy/data_center/tool/sendSms/getBusBikeTrade";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("clientId", "fcc67e0d45984be5a47147889a9d45ea");
        httpHeaders.add("clientSecret", "c20b56bdda94494d863337a0b7371589");
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        LinkedMultiValueMap<Object, Object> postParam = new LinkedMultiValueMap<>();
        HttpEntity<Object> request = new HttpEntity<>(postParam, httpHeaders);
        Map map = restTemplate.postForObject(url, request, Map.class);
        List<Map> data = (List<Map>) map.get("data");
        ArrayList<CtGjBikeTrade> objects = new ArrayList<>();
        if (!CollectionUtils.isEmpty(data)){
            data.forEach(s->{
                CtGjBikeTrade ctGjBikeTrade = new CtGjBikeTrade();
                Object tripId = s.get("tripId");
                if (tripId != null){
                    ctGjBikeTrade.setTripId(tripId.toString());
                }
                Object bikeId = s.get("bikeId");
                if (bikeId != null){
                    ctGjBikeTrade.setBikeId(bikeId.toString());
                }
                Object rentStationName = s.get("rentStationName");
                if (rentStationName != null){
                    ctGjBikeTrade.setRentStationName(rentStationName.toString());
                }
                Object restoreStationName = s.get("restoreStationName");
                if (restoreStationName != null){
                    ctGjBikeTrade.setRentStationName(restoreStationName.toString());
                }
                Object rentDate = s.get("rentDate");
                if (rentDate != null){
                    ctGjBikeTrade.setRentDate(rentDate.toString());
                }
                Object restoreDate = s.get("restoreDate");
                if (restoreDate != null){
                    ctGjBikeTrade.setRestoreDate(restoreDate.toString());
                }
                objects.add(ctGjBikeTrade);
            });
        }
        //CtGjBikeTrade ctGjBikeTrade = objects.get(0);
        //this.ctGjBikeTradeDao.insert(ctGjBikeTrade);
        log.info("公交自行车站桩数据接入->>>>>>",objects);
        this.masterSgjjtBikeTradeRepository.saveAll(objects);
    }
}
